<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class Application_Model_ArticleModel {

    private $filePath = '/xml/article.xml';

    public function getNewId($liste) {
        $v1 = 0;
        $v2 = 0;
        foreach ($liste as $l) {
            if ($l->hasAttribute("id")) {
                $v2 = $l->getAttribute('id');
                if ($v2 > $v1) {
                    $v1 = $v2;
                }
            }
        }
        return $v1 + 1;
    }

    public function readAll() {
        // action body

        $html = file_get_contents(APPLICATION_PATH . $this->filePath);
        $dom = new Zend_Dom_Query($html);
        $listDomElements = $dom->queryXpath('//article');
        return $this->getArticles($dom, $listDomElements);
    }

    public function countAll() {
        // action body

        $html = file_get_contents(APPLICATION_PATH . $this->filePath);
        $dom = new Zend_Dom_Query($html);
        $listDomElements = $dom->queryXpath('//article');
        $nb = count($listDomElements);

        return $nb;

        $this->view->xml = $listDomElements;
        $this->view->xml_nb = $nb;
    }

    public function read($id) {
        $html = file_get_contents(APPLICATION_PATH . $this->filePath);
        $dom = new Zend_Dom_Query($html);
        $article = new Application_Model_Article();

        if ($dom->queryXpath('//article[@id=' . $id . ']') == null) {
            return null;
        }

        $article->setId($id);
        $article->setDate($dom->queryXpath('//article[@id=' . $id . ']/date')->current()->textContent);
        $article->setContent($dom->queryXpath('//article[@id=' . $id . ']/content')->current()->textContent);
        $article->setOwner(new Application_Model_People(
                        $dom->queryXpath('//article[@id=' . $id . ']/owner/name')->current()->textContent,
                        $dom->queryXpath('//article[@id=' . $id . ']/owner/firstName')->current()->textContent
        ));

        foreach ($dom->queryXpath('//article[@id=' . $id . ']/picture/@url') as $picturePath) {
            $article->addPicturepath($picturePath->textContent);
        }
//        $article->addPicturepath($dom->queryXpath('//article[@id=' . $id . ']/picture/@url')->current()->textContent);
        $article->setTitle($dom->queryXpath('//article[@id=' . $id . ']/title')->current()->textContent);
        $comments = Array();
        foreach ($dom->queryXpath('//article[@id=' . $id . ']/comment') as $comment) {
            $com = new Application_Model_Comment();

            $com->setContent($comment->getElementsByTagName('content')->item(0)->textContent);
            $com->setDate($comment->getElementsByTagName('date')->item(0)->textContent);
            $name = $comment->getElementsByTagName('name')->item(0)->textContent;
            $firstName = $comment->getElementsByTagName('firstName')->item(0)->textContent;
            $author = new Application_Model_People($name, $firstName);
            $com->setAuthor($author);
            $com->setId($comment->getAttribute('id'));
            $comments[] = $com;
        }
        $article->setComments($comments);
        return $article;
    }

    public function insert($article) {

        $dom = new DOMDocument();
        $dom->load(APPLICATION_PATH . $this->filePath);


        $allArticles = $dom->getElementsByTagName('articles');
        $id = $article->getId();
        if (!isset($id) || $id == null || $id == '') {
            $id = $this->getNewId($dom->getElementsByTagName('article'));
        }
        $root = $dom->createElement('article');
        $root->setAttribute('id', $id);
        $dom->appendChild($root);
        $date = $dom->createElement('date');
        $elemtext = $dom->createTextNode($article->getDate());
        $date->appendChild($elemtext);
        $root->appendChild($date);
        $title = $dom->createElement('title');
        $elemtext = $dom->createTextNode($article->getTitle());
        $title->appendChild($elemtext);
        $root->appendChild($title);
        $owner = $dom->createElement('owner');
        $root->appendChild($owner);
        $own_name = $dom->createElement("name");
        $elemtext = $dom->createTextNode($article->getOwner()->getName());
        $own_name->appendChild($elemtext);
        $own_firstname = $dom->createElement("firstName");
        $elemtext = $dom->createTextNode($article->getOwner()->getFirstName());
        $own_firstname->appendChild($elemtext);
        $owner->appendChild($own_name);
        $owner->appendChild($own_firstname);
        $picture = $dom->createElement("picture");
        $picture->setAttribute('url', $article->getPicturepath());
        $root->appendChild($picture);
        $content = $dom->createElement("content");
        $elemtext = $dom->createTextNode($article->getContent());
        $content->appendChild($elemtext);
        $root->appendChild($content);

        $articles = $allArticles->item(0);
        $articles->appendChild($root);


        // $this->setElement($dom, $dom->root, $root);
        // Mise à jour du fichier source original
        $dom->save(APPLICATION_PATH . $this->filePath);
        //  echo APPLICATION_PATH . '/xml/ev.xml';
    }

    function delete($id) {

        $dom = new DOMDocument();
        $dom->load(APPLICATION_PATH . $this->filePath);


        $root = $dom->getElementsByTagName('articles')->item(0);
        $allArticles = $dom->getElementsByTagName('article');
        foreach ($allArticles as $article) {
            if ($article->hasAttribute("id")) {
                $v2 = $article->getAttribute('id');
                if ($v2 == $id) {
                    $root->removeChild($article);
                    $dom->save(APPLICATION_PATH . $this->filePath);
                    return true;
                }
            }
        }
        return false;
    }

    public function edit($id, $article) {
        if ($this->delete($id)) {
            $article->setId($id);
            $this->insert($article);
        }
    }

    public function addComment($id, $comment) {

        $dom = new DOMDocument();
        $dom->load(APPLICATION_PATH . $this->filePath);


        $allArticles = $dom->getElementsByTagName('article');
        $id = $comment->getId();
        if (!isset($id) || $id == null || $id == '') {
            $id = $this->getNewId($dom->getElementsByTagName('comment'));
        }
        $root = $dom->createElement('comment');
        $root->setAttribute('id', $id);
        $dom->appendChild($root);
        $date = $dom->createElement('date');
        $elemtext = $dom->createTextNode($comment->getDate());
        $date->appendChild($elemtext);
        $root->appendChild($date);
        $author_name = $dom->createElement("name");
        $elemtext = $dom->createTextNode($comment->getAuthor()->getName());
        $author_name->appendChild($elemtext);
        $root->appendChild($author_name);
        $author_firstname = $dom->createElement("firstName");
        $elemtext = $dom->createTextNode($comment->getAuthor()->getFirstName());
        $author_firstname->appendChild($elemtext);
        $root->appendChild($author_firstname);
        $content = $dom->createElement("content");
        $elemtext = $dom->createTextNode($comment->getContent());
        $content->appendChild($elemtext);
        $root->appendChild($content);

        $comments = $allArticles->item(0);
        $comments->appendChild($root);


        // $this->setElement($dom, $dom->root, $root);
        // Mise à jour du fichier source original
        $dom->save(APPLICATION_PATH . $this->filePath);
        //  echo APPLICATION_PATH . '/xml/ev.xml';
    }

    public function removeComment($id) {

        $dom = new DOMDocument();
        $dom->load(APPLICATION_PATH . $this->filePath);


        $allComments = $dom->getElementsByTagName('comment');
        foreach ($allComments as $comment) {
            if ($comment->hasAttribute("id")) {
                $v2 = $comment->getAttribute('id');
                if ($v2 == $id) {
                    $root = $comment->parentNode;
                    $root->removeChild($comment);
                    $dom->save(APPLICATION_PATH . $this->filePath);
                    return true;
                }
            }
        }
        return false;
    }

    public function addArticles($tabArticle1, $tabArticle2) {
        $found = false;
        foreach ($tabArticle2 as $article2) {
            foreach ($tabArticle1 as $article1) {
                if ($article2->getId() == $article1->getId()) {
                    $article1->increasePertinence();
                    $found = true;
                    break;
                }
            }
            if (!$found) {
                $tabArticle1[] = $article2;
            }
            $found = false;
        }
        return $tabArticle1;
    }

    public function search($key) {

        $tabArticle = $this->searchByTitle($key);
        $tabContent = $this->searchByContent($key);
        $tabArticle = $this->addArticles($tabArticle, $tabContent);
        $tabAuthor = $this->searchByAuthor($key);
        $tabArticle = $this->addArticles($tabArticle, $tabAuthor);
        $tabDate = $this->searchByDate($key);
        $tabArticle = $this->addArticles($tabArticle, $tabDate);
        usort($tabArticle, function( Application_Model_Article $a, Application_Model_Article $b) {
                    $a = $a->getPertinence();
                    $b = $b->getPertinence();
                    if ($a == $b) {
                        return 0;
                    }
                    return ($a < $b) ? -1 : 1;
                });
        return $tabArticle;
    }

    public function searchByTitle($key) {
        // action body

        $html = file_get_contents(APPLICATION_PATH . $this->filePath);
        $dom = new Zend_Dom_Query($html);
        $listDomElements = $dom->queryXpath('//article[./title[contains(.,\'' . $key . '\')]]');
        return $this->getArticles($dom, $listDomElements);
    }

    public function searchByContent($key) {
        // action body

        $html = file_get_contents(APPLICATION_PATH . $this->filePath);
        $dom = new Zend_Dom_Query($html);
        $listDomElements = $dom->queryXpath('//article[./content[contains(.,\'' . $key . '\')]]');
        return $this->getArticles($dom, $listDomElements);
    }

    public function searchByAuthor($key) {
        // action body

        $html = file_get_contents(APPLICATION_PATH . $this->filePath);
        $dom = new Zend_Dom_Query($html);
        $listDomElements = $dom->queryXpath('//article[./owner/name[contains(.,\'' . $key . '\')] or ./owner/firstname[contains(.,\'' . $key . '\')]]');
        return $this->getArticles($dom, $listDomElements);
    }

    public function searchByDate($key) {
        // action body

        $html = file_get_contents(APPLICATION_PATH . $this->filePath);
        $dom = new Zend_Dom_Query($html);
        $listDomElements = $dom->queryXpath('//article[./date[contains(.,\'' . $key . '\')]]');
        return $this->getArticles($dom, $listDomElements);
    }

    public function getArticles($dom, $listDomElements) {
        $tabArticle = Array();
        foreach ($listDomElements as $node) {
            $article = new Application_Model_Article();
            $article->setId($node->getAttribute('id'));

            $article->setDate($dom->queryXpath('//article[@id=' . $article->getId() . ']/date')->current()->textContent);
            $article->setContent($dom->queryXpath('//article[@id=' . $article->getId() . ']/content')->current()->textContent);
            $article->setOwner(new Application_Model_People($dom->queryXpath('//article[@id=' . $article->getId() . ']/owner/name')->current()->textContent, $dom->queryXpath('//article[@id=' . $article->getId() . ']/owner/firstName')->current()->textContent));
            foreach ($dom->queryXpath('//article[@id=' . $article->getId() . ']/picture/@url') as $picturePath) {
                $article->addPicturepath($picturePath->textContent);
            }
            $article->setTitle($dom->queryXpath('//article[@id=' . $article->getId() . ']/title')->current()->textContent);
            $tabArticle[] = $article;
        }

        return $tabArticle;
    }

}
?>

